/**
 * 主页进行封装
 * @author hongwei 2018-09-06
 */
(function(window){
    'use strict';
    var main = function () {
        var that = this;
        that._init();
    };

    main.prototype = {
        _init : function () {
            var that = this;
            if(typeof(api) === 'undefined'){
                console.log('该插件只能在apicloud下运行');
            }else{
                that.exitApp();         //step1:双击退出 监听
                that.orientation();     //step2:自动横屏
                that.openFrameGroup();  //step3:打开frame组
                that.menuClick();       //step4:底部按钮操作，包含step5
                //that.fnMenuClick();   //step5:点击按钮
                that.setHeight();       //step6:设置高度
            }
        }

        /**
         * step1:双击退出 监听
         */
        ,exitApp : function () {
            api.addEventListener({
                name: 'keyback'
            }, function(ret, err) {
                $toast('再按一次退出' + api.appName, 2000);
                api.addEventListener({
                    name: 'keyback'
                }, function(ret, err) {
                    api.closeWidget({
                        silent: true
                    });
                });
                //2秒后重新调用当前函数
                setTimeout(function() {
                    exitApp();
                }, 2000)
            });
        }

        /**
         * step2:自动横屏
         */
        ,orientation : function () {
            // 横屏
            api.setScreenOrientation({
                orientation: 'auto'
            });
        }

        /**
         * step3:打开frame组
         */
        , openFrameGroup : function () {

            var headerPos = $api.offset(header);

            api.openFrameGroup({
                name: 'meunFrame',
                scrollEnabled: false,
                preload: 0, //不预加载
                rect: {
                    x: 0,
                    y: 0,
                    w: 'auto',
                    h: api.winHeight - $api.byId('footer').offsetHeight
                },
                reload: false,
                index: 0,
                frames: [{
                    name: 'index',
                    url: './index/index.html',
                    bounces: false
                }, {
                    name: 'trip',
                    url: './trip/index.html',
                    bounces: false
                }, {
                    name: 'my',
                    url: './my/index.html',
                    bounces: false
                }]
            }, function(ret, err) {
                var index = ret.index; //索引值
                switch (index) {
                    case 0:
                        $("#"+ret.name).find('i').attr('class','iconfont icon-shouye1').css('color','#3cb0d7');
                        $("#trip").find('i').attr('class','iconfont icon-hangcheng2').css('color','#757575');
                        $("#my").find('i').attr('class','iconfont icon-wode1').css('color','#757575');
                        api.setFrameGroupAttr({
                            name: 'meunFrame',
                            reload: false,
                            rect: {
                                x: 0,
                                y: 0,
                                w: 'auto',
                                h: api.winHeight - $api.byId('footer').offsetHeight
                            }
                        });
                        break;
                    case 1:
                        $api.text($api.byId('header'), '行程');
                        $("#header").removeClass("aui-hide");
                        $("#"+ret.name).find('i').attr('class','iconfont icon-hangcheng').css('color','#3cb0d7');
                        $("#index").find('i').attr('class','iconfont icon-shouye1').css('color','#757575');
                        $("#my").find('i').attr('class','iconfont icon-wode1').css('color','#757575');
                        api.setFrameGroupAttr({
                            name: 'meunFrame',
                            reload: false,
                            rect: {
                                x: 0,
                                y: 70,
                                w: 'auto',
                                h: api.winHeight - $api.byId('footer').offsetHeight - $api.byId('header').offsetHeight
                            }
                        });
                        break;
                    case 2:
                        $("#header").addClass("aui-hide");
                        $("#"+ret.name).find('i').attr('class','iconfont icon-wode').css('color','#3cb0d7');
                        $("#trip").find('i').attr('class','iconfont icon-hangcheng2').css('color','#757575');
                        $("#index").find('i').attr('class','iconfont icon-shouye1').css('color','#757575');
                        api.setFrameGroupAttr({
                            name: 'meunFrame',
                            reload: false,
                            rect: {
                                x: 0,
                                y: 0,
                                w: 'auto',
                                h: api.winHeight - $api.byId('footer').offsetHeight
                            }
                        });
                        break;
                }
            });
        }

        /**
         * step4:底部按钮操作
         */
        , menuClick :function () {
            var that = this;
            //首页
            $('#index').click(function(){
                that.fnMenuClick(0, this);
            });
            //行程
            $('#trip').click(function(){
                that.fnMenuClick(1, this);
            });

            //我的
            $('#my').click(function(){
                that.fnMenuClick(2, this);
            });
        }

        /**
         * step5:点击按钮
         * @param nLocation
         * @param tag
         */
        , fnMenuClick : function(nLocation, tag) {
            if ($(tag).hasClass("active")) return;
            api.setFrameGroupIndex({
                name: 'meunFrame',
                index: nLocation,
                scroll: true,
                reload: false
            });
            //点击选中特效
            $(tag).addClass("active").siblings().removeClass("active");
        }

        /**
         * step6:设置高度
         */
        ,setHeight : function () {
            var header = $api.byId('header');
            $api.fixStatusBar(header);

            $api.fixTabBar($api.byId('footer'));
        }
    };
    window.main = main;
})(window);
